package com.ruoyi.project.shortUrl.third.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.shortUrl.third.domain.ThirdShort;
import com.ruoyi.project.shortUrl.third.mapper.ThirdShortMapper;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-04-18
 */
@Controller
@RequestMapping("/short/third")
public class ThirdShortController extends BaseController
{
    private String prefix = "shortUrl/third";

    @Autowired
    private ThirdShortMapper mapper;

    @RequiresPermissions("short:third:view")
    @GetMapping()
    public String shorts()
    {
        return prefix + "/third";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("short:third:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ThirdShort thirdShort)
    {
        startPage();
        List<ThirdShort> list = mapper.selectThirdShortList(thirdShort);
        return getDataTable(list);
    }


    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("short:third:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ThirdShort thirdShort)
    {
    	if(thirdShort.getUseDefault()) {
    		List<ThirdShort> list = mapper.selectThirdShortList(null);
    		for(ThirdShort t:list) {
    			t.setUseDefault(false);
    			mapper.updateThirdShort(t);
    		}
    	}
        return toAjax(mapper.insertThirdShort(thirdShort));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ThirdShort thirdShort = mapper.selectThirdShortById(id);
        mmap.put("thirdShort", thirdShort);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("short:third:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ThirdShort thirdShort)
    {
    	if(thirdShort.getUseDefault()) {
    		List<ThirdShort> list = mapper.selectThirdShortList(null);
    		for(ThirdShort t:list) {
    			t.setUseDefault(false);
    			mapper.updateThirdShort(t);
    		}
    		thirdShort.setUseDefault(true);
    	}
        return toAjax(mapper.updateThirdShort(thirdShort));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("short:third:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mapper.deleteThirdShortByIds(Convert.toStrArray(ids)));
    }
}
